package org.dromara.comm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsActivity;
import org.dromara.comm.domain.AsActivityImg;
import org.dromara.comm.domain.AsEventRegistration;
import org.dromara.comm.domain.AsRegistrationInformation;
import org.dromara.comm.domain.dto.ActivityDto;
import org.dromara.comm.domain.dto.ActivityImgDto;
import org.dromara.comm.domain.dto.RegistrationDto;
import org.dromara.comm.mapper.AsActivityImgMapper;
import org.dromara.comm.mapper.AsActivityMapper;
import org.dromara.comm.mapper.AsEventRegistrationMapper;
import org.dromara.comm.mapper.AsRegistrationInformationMapper;
import org.dromara.comm.service.AsRegistrationInformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的报名业务层
 *
 * @author: 郭伟明
 * @date: 2024-08-06 10:09
 **/
@Service
public class AsRegistrationInformationServiceImpl extends ServiceImpl<AsRegistrationInformationMapper, AsRegistrationInformation> implements AsRegistrationInformationService {

    @Autowired
    private AsRegistrationInformationMapper asRegistrationInformationMapper;

    @Autowired
    private AsActivityMapper asActivityMapper;

    @Autowired
    private AsActivityImgMapper asActivityImgMapper;

    @Autowired
    private AsEventRegistrationMapper asEventRegistrationMapper;

    /**
     * 我要报名
     * @param asRegistrationInformation
     */
    @Override
    public void insertAsRegistrationInformation(AsRegistrationInformation asRegistrationInformation) {
        asRegistrationInformationMapper.insert(asRegistrationInformation);
    }

    /**
     * 添加活动
     * @param asActivity
     * @param asActivityImg
     */
    @Override
    public void insetAsActivity(AsActivity asActivity, AsActivityImg asActivityImg) {
        AsActivityImg activityImg = asActivityImgMapper.selectById(asActivityImg.getId());
        if (activityImg != null) {
            asActivity.setThumbnail(activityImg.getUrl());
            asActivityMapper.insert(asActivity);
        }
    }

    /**
     * 查询我的活动
     * @param asActivity
     * @return
     */
    @Override
    public List<Object> queryActivity(AsActivity asActivity) {
        List<Object> list = new ArrayList<>();
        List<AsEventRegistration> asRegistrationInformations = asEventRegistrationMapper.selectList(null);

        for (AsEventRegistration asRegistrationInformation : asRegistrationInformations) {
            LambdaQueryWrapper<AsActivity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AsActivity::getId, asRegistrationInformation.getActivityId());
            List<AsActivity> asActivities = asActivityMapper.selectList(wrapper);
                list.add(asActivities);
            }
        return list;
    }

    @Override
    public List<ActivityDto> selectActivity(AsActivity asActivity) {
        List<ActivityDto> list = new ArrayList<>();
        List<AsEventRegistration> asRegistrationInformations = asEventRegistrationMapper.selectList(null);
        for (AsEventRegistration asRegistrationInformation : asRegistrationInformations) {
            LambdaQueryWrapper<AsActivity> activityWrapper = new LambdaQueryWrapper<>();
            activityWrapper.eq(AsActivity::getId, asRegistrationInformation.getActivityId());
            List<AsActivity> asActivities = asActivityMapper.selectList(activityWrapper);
            for (AsActivity activity : asActivities) {
                ActivityDto activityDTO = new ActivityDto();
                BeanUtils.copyProperties(activity, activityDTO);
                LambdaQueryWrapper<AsActivityImg> imgWrapper = new LambdaQueryWrapper<>();
                imgWrapper.eq(AsActivityImg::getActivityId, activity.getId());
                List<AsActivityImg> asActivityImgs = asActivityImgMapper.selectList(imgWrapper);
                List<ActivityImgDto> activityImgDTOList = new ArrayList<>();
                for (AsActivityImg img : asActivityImgs) {
                    ActivityImgDto imgDTO = new ActivityImgDto();
                    BeanUtils.copyProperties(img, imgDTO);
                    activityImgDTOList.add(imgDTO);
                }
                RegistrationDto registrationDTO = new RegistrationDto();
                registrationDTO.setId(asRegistrationInformation.getId());
                registrationDTO.setActivityId(asRegistrationInformation.getActivityId());
                registrationDTO.setName(asRegistrationInformation.getName());
                registrationDTO.setPhone(asRegistrationInformation.getPhone());
                registrationDTO.setSex(asRegistrationInformation.getSex());
                registrationDTO.setAge(asRegistrationInformation.getAge());
                registrationDTO.setState(asRegistrationInformation.getState());
                registrationDTO.setImages(activityImgDTOList);
                List<RegistrationDto> registrationDTOList = new ArrayList<>();
                registrationDTOList.add(registrationDTO);
                activityDTO.setAsEventRegistration(registrationDTOList);
                list.add(activityDTO);
            }
        }
        return list;
    }


}
